package org.tnc.budget.webapp.controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.tnc.budget.model.BudgetCenter;
import org.tnc.budget.model.EventTable;
import org.tnc.budget.service.BudgetManager;
import org.tnc.budget.service.EventManager;
import org.tnc.budget.webapp.util.AuthCryptException;
import org.tnc.budget.webapp.util.DESHelper;
import org.tnc.model.User;
import org.tnc.service.UserManager;

/**
 * 
 * @author houjj | 2011-4-16 下午02:01:15
 */
@Controller
public class DownloadController {

	private Logger log = Logger.getLogger(DownloadController.class);
	private UserManager userManager;

	private EventManager eventManager;

	private BudgetManager budgetManager;

	public void setEventManager(EventManager eventManager) {
		this.eventManager = eventManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	/**
	 * 创建可变的表头
	 * 
	 * @param workbook
	 * @param header1
	 * @param header2
	 * @param header3
	 * @param bcList
	 *            - 预算中心
	 * @param vacationList
	 *            - 假期（一种特殊的预算中心）
	 */
	private void createHeader(XSSFWorkbook wb, XSSFRow header1,
			XSSFRow header2, XSSFRow header3, Collection<BudgetCenter> bcList,
			Collection<BudgetCenter> vacationList) {
		CellStyle csh1 = wb.createCellStyle();
		csh1.setBorderTop(CellStyle.BORDER_THICK);
		csh1.setTopBorderColor(IndexedColors.BLACK.getIndex());
		csh1.setBorderLeft(CellStyle.BORDER_THIN);
		csh1.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		/*
		 * csh1.setBorderBottom(CellStyle.BORDER_THIN);
		 * csh1.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		 */
		csh1.setBorderRight(CellStyle.BORDER_THIN);
		csh1.setRightBorderColor(IndexedColors.BLACK.getIndex());

		CellStyle csh2 = wb.createCellStyle();
		/*
		 * csh2.setBorderTop(CellStyle.BORDER_THIN);
		 * csh2.setTopBorderColor(IndexedColors.BLACK.getIndex());
		 */
		csh2.setBorderLeft(CellStyle.BORDER_THIN);
		csh2.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		/*
		 * csh2.setBorderBottom(CellStyle.BORDER_THIN);
		 * csh2.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		 */
		csh2.setBorderRight(CellStyle.BORDER_THIN);
		csh2.setRightBorderColor(IndexedColors.BLACK.getIndex());

		CellStyle csh3 = wb.createCellStyle();
		/*
		 * csh3.setBorderTop(CellStyle.BORDER_THIN);
		 * csh3.setTopBorderColor(IndexedColors.BLACK.getIndex());
		 */
		csh3.setBorderLeft(CellStyle.BORDER_THIN);
		csh3.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		csh3.setBorderBottom(CellStyle.BORDER_THICK);
		csh3.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		csh3.setBorderRight(CellStyle.BORDER_THIN);
		csh3.setRightBorderColor(IndexedColors.BLACK.getIndex());

		XSSFCell c1 = header1.createCell(0);
		XSSFCell c2 = header2.createCell(0);
		XSSFCell c3 = header3.createCell(0);
		c1.setCellStyle(csh1);
		c2.setCellStyle(csh2);
		c3.setCellStyle(csh3);
		c1.setCellValue("Day of");
		c3.setCellValue("Month");

		int i = 0;
		for (BudgetCenter bc : bcList) {
			c1 = header1.createCell(i + 1);
			c2 = header2.createCell(i + 1);
			c3 = header3.createCell(i + 1);
			c1.setCellStyle(csh1);
			c2.setCellStyle(csh2);
			c3.setCellStyle(csh3);
			if (i == 0) {
				c1.setCellValue("Budget Center Number And Name");
			}
			c2.setCellValue(bc.getBcId());
			c3.setCellValue(bc.getBcName());
			i++;
		}

		int column = bcList.size() + 1;

		c1 = header1.createCell(column);
		c2 = header2.createCell(column);
		c3 = header3.createCell(column);
		c1.setCellStyle(csh1);
		c2.setCellStyle(csh2);
		c3.setCellStyle(csh3);
		c2.setCellValue("Hours");
		c3.setCellValue("Subtotal");
		column++;

		for (BudgetCenter v : vacationList) {
			c1 = header1.createCell(column);
			c2 = header2.createCell(column);
			c3 = header3.createCell(column);
			c1.setCellStyle(csh1);
			c2.setCellStyle(csh2);
			c3.setCellStyle(csh3);
			c3.setCellValue(v.getBcName());
			column++;
		}

		c1 = header1.createCell(column);
		c2 = header2.createCell(column);
		c3 = header3.createCell(column);
		c1.setCellStyle(csh1);
		c2.setCellStyle(csh2);
		c3.setCellStyle(csh3);
		c2.setCellValue("Total");
		c3.setCellValue("Hours");
		column++;
	}

	/**
	 * 
	 */
	@RequestMapping(value = "/download.do")
	public void download(HttpServletRequest request,
			HttpServletResponse response) {
		// 请求 email地址，月份这个是用密码加密过的数据 ，加密(2011-04,时间戳)
		// 返回xls文件

		printRequestParams(request);

		String emailParam = "";
		String authParam = "";
		StringBuffer result = new StringBuffer();

		User u = null;
		String date = "";
		try {
			emailParam = ServletRequestUtils.getStringParameter(request,
					"email");
			authParam = ServletRequestUtils.getStringParameter(request, "data");
			if (emailParam == null) {
				throw new ServletRequestBindingException("parameter 'email'");
			}

			if (authParam == null) {
				throw new ServletRequestBindingException("parameter 'data'");
			}

			u = userManager.getUserByUserEmail(emailParam);
			String pwd = u.getPassword();
			String newPwd = pwd.substring(5, 13);
			String ddata = DESHelper.desDecrypt(authParam, newPwd);
			if (log.isDebugEnabled())
				log.debug("Decrypted Data:" + ddata);

			String[] data = ddata.split(",");
			date = data[0];

		} catch (ServletRequestBindingException e) {
			log.warn("Parameter 'email' or 'data' not exist, " + e.getMessage());
			result.append(-4);
		} catch (UsernameNotFoundException e) {
			log.error("User not exist");
			result.append(-2);
		} catch (AuthCryptException e) {
			log.error("Decrypt Error");
			result.append(-3);
		} catch (Exception e) {
			log.error("Download file error", e);
			result.append(-1);
		}

		if (result.length() > 0) {
			// 返回错误消息
			try {
				PrintWriter pw = response.getWriter();
				pw.write(result.toString());
				pw.close();
			} catch (IOException e) {
				log.error(
						"Send error result message error:" + result.toString(),
						e);
			}

		} else {
			// 返回数据

			// 测试
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition",
					"attachment;filename=aaa.xlsx");

			ServletOutputStream sos = null;
			try {
				sos = response.getOutputStream();
				int year = Integer.parseInt(date.split("-")[0]);
				int month = Integer.parseInt(date.split("-")[1]);
				long name = u.getId();

				List<EventTable> events = eventManager.getEventByMonth(year,
						month, name);

				Set<BudgetCenter> bcset = new LinkedHashSet<BudgetCenter>();
				Set<BudgetCenter> vacationSet = new LinkedHashSet<BudgetCenter>();

				vacationSet.addAll(budgetManager.getAll(true, true));

				for (EventTable et : events) {
					BudgetCenter bc = et.getBudgetCenter();
					if (bc.getVacation() == null || !bc.getVacation())
						bcset.add(bc);
				}

				List<BudgetCenter> bclist = new LinkedList<BudgetCenter>(bcset);
				List<BudgetCenter> vacationList = new LinkedList<BudgetCenter>(
						vacationSet);
				Map<Integer, String> requiredExplanation = new HashMap<Integer, String>();

				if (log.isDebugEnabled())
					log.debug(u.getUsername() + " " + date + " events: "
							+ events.size());

				InputStream template = request
						.getSession()
						.getServletContext()
						.getResourceAsStream(
								"/WEB-INF/xlsx_template/employee_details.xlsx");
				XSSFWorkbook wb = new XSSFWorkbook(template);
				IOUtils.closeQuietly(template);

				XSSFSheet sheet = wb.getSheet("Monthly Time Report");
				XSSFRow row4 = sheet.getRow(3);

				XSSFCell printName = row4.getCell(2);
				printName
						.setCellValue(u.getFirstName() + "," + u.getLastName());
				XSSFCell employeeNumber = row4.getCell(9);
				employeeNumber.setCellType(XSSFCell.CELL_TYPE_STRING);
				employeeNumber.setCellValue(u.getWorkId());

				XSSFRow row5 = sheet.getRow(4);
				XSSFCell monthcell = row5.getCell(0);
				monthcell.setCellValue("For The Month Ending:    " + month
						+ " Year " + year);
				XSSFCell workLoc = row5.getCell(9);
				workLoc.setCellType(XSSFCell.CELL_TYPE_STRING);
				if (StringUtils.isNotBlank(u.getAddress())) {
					workLoc.setCellValue(u.getAddress());
				} else {
					workLoc.setCellValue("暂无，请联系管理员设置。");
				}

				// 创建月明细表头
				int monthHeaderStart = 8;
				XSSFRow header1 = sheet.createRow(monthHeaderStart);
				XSSFRow header2 = sheet.createRow(monthHeaderStart + 1);
				XSSFRow header3 = sheet.createRow(monthHeaderStart + 2);

				createHeader(wb, header1, header2, header3, bclist,
						vacationList);

				// Day of month start row.
				int domStartRow = 11;

				int columnNumber = bclist.size() + vacationList.size() + 3; // day
																			// of
																			// month,
																			// hours
																			// subtotal,
																			// holiday,
																			// annual
																			// leave...

				Calendar calendar = Calendar.getInstance();
				calendar.set(year, month - 1, 1);
				int maxDaysMonth = calendar
						.getActualMaximum(Calendar.DAY_OF_MONTH);
				for (int i = 0; i < maxDaysMonth; i++) {
					calendar.set(Calendar.DAY_OF_MONTH, i + 1);

					XSSFRow domrow = sheet.createRow(domStartRow + i);

					CellStyle cellStyle = wb.getStylesSource()
							.createCellStyle();
					cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
					cellStyle.setBottomBorderColor(IndexedColors.BLACK
							.getIndex());
					cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
					cellStyle
							.setLeftBorderColor(IndexedColors.BLACK.getIndex());
					cellStyle.setBorderRight(CellStyle.BORDER_THIN);
					cellStyle.setRightBorderColor(IndexedColors.BLACK
							.getIndex());
					cellStyle.setBorderTop(CellStyle.BORDER_THIN);
					cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
					cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

					int dayOfweek = calendar.get(Calendar.DAY_OF_WEEK);
					if (dayOfweek == Calendar.SUNDAY
							|| dayOfweek == Calendar.SATURDAY) {
						cellStyle
								.setFillForegroundColor(IndexedColors.GREY_25_PERCENT
										.getIndex());
					} else {
						cellStyle.setFillForegroundColor(IndexedColors.WHITE
								.getIndex());
					}

					for (int j = 0; j < columnNumber; j++) {
						XSSFCell c = domrow.createCell(j);
						c.setCellStyle(cellStyle);
						c.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
						c.setCellValue(0);
						if (j == bclist.size() + 1) {
							if (!bclist.isEmpty()) {
								XSSFCell bcs = domrow.getCell(1);
								XSSFCell bce = domrow.getCell(bclist.size());
								c.setCellFormula("SUM(" + bcs.getReference()
										+ ":" + bce.getReference() + ")");
							} else {
								c.setCellValue(0);
							}
						}
						if (j == columnNumber - 1) {
							XSSFCell t1 = domrow.getCell(bclist.size() + 1);
							XSSFCell t2 = domrow.getCell(bclist.size()
									+ vacationList.size() + 1);
							c.setCellFormula("SUM(" + t1.getReference() + ":"
									+ t2.getReference() + ")");
						}
						if (j == 0) {
							c.setCellType(XSSFCell.CELL_TYPE_STRING);
							c.setCellValue(i + 1);
						}
					}
				}

				CellStyle cellStyle = wb.getStylesSource().createCellStyle();
				cellStyle.setBorderBottom(CellStyle.BORDER_THICK);
				cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
				cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
				cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
				cellStyle.setBorderRight(CellStyle.BORDER_THIN);
				cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
				cellStyle.setBorderTop(CellStyle.BORDER_THICK);
				cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
				cellStyle
						.setFillForegroundColor(IndexedColors.WHITE.getIndex());
				cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

				XSSFRow totalRow = sheet.createRow(maxDaysMonth + domStartRow);
				XSSFCell cell = totalRow.createCell(0);
				cell.setCellStyle(cellStyle);
				cell.setCellValue("Total");

				Pattern a1pat = Pattern.compile("([A-Z]+)([0-9]+)");

				List<String> totalRefs = new LinkedList<String>();
				for (int j = 1; j < columnNumber; j++) {
					cell = totalRow.createCell(j);
					cell.setCellStyle(cellStyle);
					String ref = cell.getReference();
					totalRefs.add(ref);
					Matcher m = a1pat.matcher(ref);
					if (m.find()) {
						String cl = m.group(1);
						cell.setCellFormula("SUM(" + (cl + (domStartRow + 1))
								+ ":" + (cl + (domStartRow + maxDaysMonth))
								+ ")");
					}
				}

				for (EventTable eventTable : events) {
					BudgetCenter bc = eventTable.getBudgetCenter();
					int day = Integer.parseInt(eventTable.getStartDate().split(
							"-")[2]);
					XSSFRow r = sheet.getRow(domStartRow + day - 1);
					int bci = bclist.indexOf(bc);
					if (bci < 0) {
						//Issue 23, Admin Leave 和 的备注
						if ("Admin Leave".equalsIgnoreCase(bc.getBcName())
								|| "Holiday".equalsIgnoreCase(bc.getBcName())) {
							String exp = requiredExplanation.get(bc.getId());
							if (exp == null) {
								exp = eventTable.getStartDate() + ":" + eventTable.getEventText();
							} else {
								exp += ". \n" + eventTable.getStartDate() + ":" + eventTable.getEventText();
							}
							requiredExplanation.put(bc.getId(), exp);
						}
						
						int vi = vacationList.indexOf(bc);
						XSSFCell c = r.getCell(vi + bclist.size() + 2);
						double totalDay = c.getNumericCellValue()
								+ (eventTable).getDuration();
						c.setCellValue(totalDay);
					} else {
						XSSFCell c = r.getCell(bci + 1);
						double totalDay = c.getNumericCellValue()
								+ (eventTable).getDuration();
						c.setCellValue(totalDay);
					}
				}

				int recapRowIndex = maxDaysMonth + domStartRow + 2;
				XSSFRow recapRow = sheet.createRow(recapRowIndex);

				XSSFCell recapLabel = recapRow.createCell(0);
				recapLabel.setCellValue("Recap:");

				CellStyle cellStyleThin = wb.getStylesSource()
						.createCellStyle();
				cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
				cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
				cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
				cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
				cellStyle.setBorderRight(CellStyle.BORDER_THIN);
				cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
				cellStyle.setBorderTop(CellStyle.BORDER_THIN);
				cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
				cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

				for (int j = 1; j < 9; j++) {
					XSSFCell titleCell = recapRow.createCell(j);
					titleCell.setCellStyle(cellStyleThin);
					if (j == 1) {
						titleCell.setCellValue("Budget Center");
					} else if (j == 6) {
						titleCell.setCellValue("Total");
					}
				}

				sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
						recapRowIndex, 1, 5));
				sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
						recapRowIndex, 7, 8));

				recapRow = sheet.createRow(++recapRowIndex);
				for (int j = 0; j < 10; j++) {
					XSSFCell titleCell = recapRow.createCell(j);
					titleCell.setCellStyle(cellStyleThin);
					if (j == 1) {
						titleCell.setCellValue("Name");
					} else if (j == 4) {
						titleCell.setCellValue("Number");
					} else if (j == 6) {
						titleCell.setCellValue("Hours");
					} else if (j == 7) {
						titleCell.setCellValue("Pay Allocation");
					} else if (j == 9) {
						titleCell.setCellValue("Required Explanations:");
					}
				}
				sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
						recapRowIndex, 1, 3));
				sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
						recapRowIndex, 4, 5));
				sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
						recapRowIndex, 7, 8));

				CellStyle csAllocSal = wb.createCellStyle();
				csAllocSal.cloneStyleFrom(cellStyleThin);
				csAllocSal.setDataFormat(wb.createDataFormat().getFormat("0%"));

				List<String> recapthRefs = new LinkedList<String>();

				int bci = 0;
				for (BudgetCenter bc : bclist) {
					recapRow = sheet.createRow(++recapRowIndex);

					for (int j = 0; j < 10; j++) {
						XSSFCell titleCell = recapRow.createCell(j);
						titleCell.setCellStyle(cellStyleThin);
						if (j == 1) {
							titleCell.setCellValue(bc.getBcName());
						} else if (j == 4) {
							titleCell.setCellValue(bc.getBcId());
						} else if (j == 6) {
							titleCell.setCellFormula(totalRefs.get(bci));
							recapthRefs.add(titleCell.getReference());
						} else if (j == 7) {
							titleCell.setCellStyle(csAllocSal);
							titleCell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
							titleCell.setCellFormula(recapRow.getCell(6)
									.getReference()
									+ "/"
									+ totalRefs.get(totalRefs.size() - 1));
						} else if (j == 9) {
							if (requiredExplanation.get(bc.getId()) != null)
								titleCell.setCellValue(requiredExplanation.get(bc.getId()));
						}
					}

					sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
							recapRowIndex, 1, 3));
					sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
							recapRowIndex, 4, 5));
					sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
							recapRowIndex, 7, 8));

					bci++;
				}

				bci += 1;
				for (BudgetCenter bc : vacationList) {
					recapRow = sheet.createRow(++recapRowIndex);

					for (int j = 0; j < 10; j++) {
						XSSFCell titleCell = recapRow.createCell(j);
						titleCell.setCellStyle(cellStyleThin);
						if (j == 1) {
							titleCell.setCellValue(bc.getBcName());
						} else if (j == 4) {
							titleCell.setCellValue(bc.getBcId());
						} else if (j == 6) {
							titleCell.setCellFormula(totalRefs.get(bci));
							recapthRefs.add(titleCell.getReference());
						} else if (j == 7) {
							titleCell.setCellStyle(csAllocSal);
							titleCell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
							titleCell.setCellFormula(recapRow.getCell(6)
									.getReference()
									+ "/"
									+ totalRefs.get(totalRefs.size() - 1));
						} else if (j == 9) {
							if (requiredExplanation.get(bc.getId()) != null)
								titleCell.setCellValue(requiredExplanation.get(bc.getId()));
						}
					}

					sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
							recapRowIndex, 1, 3));
					sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
							recapRowIndex, 4, 5));
					sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
							recapRowIndex, 7, 8));

					bci++;
				}
				recapRow = sheet.createRow(++recapRowIndex);

				for (int j = 0; j < 10; j++) {
					XSSFCell titleCell = recapRow.createCell(j);
					titleCell.setCellStyle(cellStyleThin);
					if (j == 1) {
						titleCell.setCellValue("total");
					} else if (j == 4) {
					} else if (j == 6) {
						titleCell.setCellFormula("SUM(" + recapthRefs.get(0)
								+ ":" + recapthRefs.get(recapthRefs.size() - 1)
								+ ")");
					} else if (j == 7) {
						titleCell.setCellStyle(csAllocSal);
						titleCell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
						titleCell.setCellFormula(recapRow.getCell(6)
								.getReference()
								+ "/"
								+ totalRefs.get(totalRefs.size() - 1));
					}
				}

				sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
						recapRowIndex, 1, 3));
				sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
						recapRowIndex, 4, 5));
				sheet.addMergedRegion(new CellRangeAddress(recapRowIndex,
						recapRowIndex, 7, 8));

				wb.write(sos);

				/*
				 * 
				 * String rootPath =
				 * request.getSession().getServletContext().getRealPath("/");
				 * fis = new FileInputStream(rootPath+"/aaa.xlsx"); int n = 0;
				 * while ((n = fis.read(temp)) != -1) { sos.write(temp, 0, n);
				 * len += n; }
				 */

				// response.setHeader("Content-Length", String.valueOf(len));
				sos.flush();

			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (sos != null)
					try {
						sos.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		}

	}

	private void printRequestParams(HttpServletRequest request) {
		Map params = request.getParameterMap();
		Set s = params.keySet();
		Iterator sit = s.iterator();
		while (sit.hasNext()) {
			Object o = sit.next();
			String[] v = (String[]) params.get(o);
			log.info(o + "=" + v[0]);
		}
	}

	public void setBudgetManager(BudgetManager budgetManager) {
		this.budgetManager = budgetManager;
	}
}
